package com.luping.xiao.xiao.utils.Thread;

import com.luping.xiao.xiao.utils.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;
import java.util.TimerTask;

public class TimerRun {
    protected static final Logger log = LoggerFactory.getLogger(TimerRun.class);
    private static Integer totalExecuteSeconds = 0;
    private static Timer timer = null;


    public static void init() {
        log.info("init");
        totalExecuteSeconds++;
        ThreadRunConsumer threadRunConsumer = SpringUtil.getBean(ThreadRunConsumer.class);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (ThreadQueue.currentTaskStat == ThreadQueue.EXTRACTOR_STAT.ANALYSIS_DONE) {
                    log.info("数据解析完成"+ThreadQueue.waitSeconds);
                    ThreadQueue.waitSeconds++;
                    if (ThreadQueue.WaitTime == ThreadQueue.waitSeconds) {
                        log.info("开始添加数据");
                        threadRunConsumer.insertList();
                    }
                } else {
                    ThreadQueue.waitSeconds = 0;
                }
                if (ThreadQueue.currentTaskStat == ThreadQueue.EXTRACTOR_STAT.STORE_DONE) {
                    ThreadQueue.currentTaskStat = ThreadQueue.EXTRACTOR_STAT.WAITSTART;
                    log.info("当前任务,执行结束，共耗时：" + (++totalExecuteSeconds));
                    ThreadQueue.waitSeconds = 0;
                    timer.cancel();
                    timer = null;
                }

            }
        }, 0, 1000);
    }
}
